$().ready(function() {
	validateRule();
});
$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/commanderList/horn/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				if (data.msg) {
					parent.layer.alert(data.msg)
				}
			}

		}
	});
}


function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules: {
            deviceId: {
                required: true
            }
        },
        messages: {
            deviceId: {
                required: icon + "编号不能为空"
            }
		}
	})
}

/**
 * 设置选择的商品信息
 * @param productId
 * @param title
 */
function loadProductInfo(productId, title) {
	$('#productId').val(productId);
	$('#productName').val(title);
}

/**
 * 商品选择点击
 */
function selectProduct() {
    var centreId = $("*[name='centreId']").val();
	layer.open({
		id: "ticket_limit_product",
		type: 2,
		title: "绑定商品",
		maxmin: true,
		shadeClose: true,
		area: ['70%', '80%'],
		content: "/promotion/ticket/limitProduct?centreId=" + centreId
	})
}


function onThumbBlur() {
	var url = $.trim($('#thumb').val());
	if (url) {
		if (url.indexOf("http") >= 0 || url.indexOf("https") >= 0) {
			$('#categoryAddthumbImg').attr('src', url);
		}else {
			var prefix = "http://pic.jinzhishenghuo.com";
			$('#categoryAddthumbImg').attr('src', prefix + url);
		}
	}
}

function loadImgsInfo(domId, result) {
    if (!result || result.length <1) {
        parent.layer.msg("上传图片失败!",{icon:7});
        return;
    }
    $('#'+domId).val(result.join(","));
    /*var thumbUrl = result.split(",");
    for (var i = 0;i<thumbUrl.length;i++) {
    	var html = '<img class="img-responsive" id="categoryAddthumbImg" src="'+thumbUrl[i]+'" style="max-width: 30%">';
        $('#imgDiv').append(html);
    }*/

}

//批量上传图片
function selectImgs(domId) {
    layer.open({
        id:'selectImgs',
        type : 2,
        title : '批量选择图片',
        maxmin : true,
        shadeClose : false,
        area : [ '90%', '90%' ],
        content : '/platform/config/batchUploadImg/' + domId // iframe的url
    });
}


/**
 * 分类选择变更
 * @param category
 */
function categoryChange(category) {
	var categoryId = category.value;
	if (categoryId) {
		$.ajax({
			url:"/category/manage/ccates",
			type:"POST",
			data:{
				parentId: categoryId
			},
			dataType:'JSON',
			error : function(request) {
				parent.layer.alert("Connection error");
			},
			success:function (data) {
				if (data.code == 0) {
					var ccates = data.ccates;
					if (ccates) {
						$('#ccate').empty();
						$('#ccate').append("<option value>" + "二级分类" + "</option>");
						for (var i =0; i< ccates.length; i++) {
							var item = ccates[i];
							$('#ccate').append("<option value='" + item.id + "'>" + item.name + "</option>");
						}
					}
				}
			}
		})
	}
}